home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 January / enter-2004-01.iso / files / maxima-5.9.0.exe / {app} / share / maxima / 5.9.0 / src / mload.lisp < prev    next >
Encoding:
Text File  |  2003-02-09  |  29.9 KB  |  930 lines

  1. ;;; -*-  Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;;     The data in this file contains enhancments.                    ;;;;;
  4. ;;;                                                                    ;;;;;
  5. ;;;  Copyright (c) 1984,1987 by William Schelter,University of Texas   ;;;;;
  6. ;;;     All rights reserved                                            ;;;;;
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  9. ;;;       (c) Copyright 1982 Massachusetts Institute of Technology       ;;;
  10. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  11.  
  12. (in-package "MAXIMA")
  13. (macsyma-module mload)
  14.  
  15. ;; I decided to move most of the file hacking utilities I used in TRANSL to
  16. ;; this file. -GJC
  17.  
  18. ;; Concepts:
  19. ;; Lisp_level_filename. Anything taken by the built-in lisp I/O primitives.
  20. ;;
  21. ;; User_level_filename. Comes through the macsyma reader, so it has an extra "&"
  22. ;;   in the pname in the case of "filename" or has extra "$" and has undergone
  23. ;;   ALIAS transformation in the case of 'FOOBAR or '[FOO,BAR,BAZ].
  24. ;;
  25. ;; Canonical_filename. Can be passed to built-in lisp I/O primitives, and
  26. ;;   can also be passed back to the user, is specially handled by the DISPLAY.
  27. ;;
  28. ;; Functions:
  29. ;; $FILENAME_MERGE. Takes User_level_filename(s) and Canonical_filename(s) and
  30. ;;   merges them together, returning a Canonical_filename.
  31. ;;
  32. ;; TO-MACSYMA-NAMESTRING. Converts a Lisp_level_filename to a Canonical_filename
  33. ;;
  34. ;; $FILE_SEARCH ............ Takes a user or canonical filename and a list of types of
  35. ;;                           applicable files to look for.
  36. ;; $FILE_TYPE   ............. Takes a user or canonical filename and returns
  37. ;;                            NIL, $MACSYMA, $LISP, or $FASL.
  38. ;; CALL-BATCH1 ............. takes a canonical filename and a no-echop flag.
  39.  
  40. ;; Note: This needs to be generalized some more to take into account
  41. ;; the lispmachine situation of access to many different file systems
  42. ;; at the same time without, and also take into account the way it presently
  43. ;; deals with that situation. The main thing wrong now is that the file-default
  44. ;; strings are constants.
  45.  
  46. ;; What a cannonical filename is on the different systems:
  47. ;; This is for informational purposes only, as the Macsyma-Namestringp
  48. ;; predicate is provided.
  49. ;; [PDP-10 Maclisp] An uninterned symbol with various properties.
  50. ;; [Franz Lisp] a string or a symbol (whose print name is used).
  51. ;; [Multics Maclisp] A STRING.
  52. ;; [LispMachine] A generic pathname object, which is a system-provided FLAVOR.
  53. ;; [NIL] Not decided yet, but a STRING should do ok, since in NIL files are
  54. ;; a low-level primitive, and programs, modules, and environments are the
  55. ;; practical abstraction used. No attempt is made to come up with ad-hoc generalizations
  56. ;; of the ITS'ish and DEC'ish filenames, as such attempts fail miserably to provide
  57. ;; the functionality of filesystems such as on Multics.
  58.  
  59. ;------
  60. ;There is also this problem of what file searching and defaulting means,
  61. ; especially when this is done across systems.  My feeling right now
  62. ; is that searching might actually be three-layered:
  63. ; host, device/directory, type [assuming a name is given].  This will become
  64. ; important in the near future because NIL is going to be supporting
  65. ; multiple hosts over DECNET, using the common-lisp/lispm model of pathnames
  66. ; and hosts.  One of the problems with incremental merging algorithms like
  67. ; are used here is that making a pathname the first time tends to force an
  68. ; interpretation with respect to some specific host, which is probably
  69. ; defaulted from some place used for ultimate defaulting and not normally
  70. ; used by higher-level facilities like LOAD.  --gsb
  71. ;------
  72.  
  73. (declare-top (SPECIAL $FILE_SEARCH_LISP $FILE_SEARCH_MAXIMA $FILE_SEARCH_DEMO))
  74.  
  75.  
  76. (DEFMFUN $LISTP_CHECK (VAR VAL)
  77.   "Gives an MAXIMA-ERROR message including its first argument if its second
  78.   argument is not a LIST"
  79.   (OR ($LISTP VAL)
  80.       (MERROR "The variable ~:M being set to a non-LISTP object:~%~M"
  81.           VAR VAL)))
  82.  
  83. (DEFPROP $FILE_SEARCH $LISTP_CHECK ASSIGN)
  84.  
  85. (DEFPROP $FILE_TYPES $LISTP_CHECK ASSIGN)
  86.  
  87. #-(or Franz cl)
  88. (DEFMFUN $FILE_SEARCH (X &OPTIONAL
  89.              (consp NIL)
  90.              (L $FILE_TYPES))
  91.   (SETQ X ($FILENAME_MERGE X))
  92.   (IF ($LISTP L) (SETQ L (CDR L))
  93.       (MERROR "3'rd arg to FILE_SEARCH not a list.~%~M" L))
  94.   (DO ((MERGE-SPECS ;(CONS ($filename_merge)
  95.             ;      ;; Get a complete "star filename"
  96.             ;      (CDR $FILE_SEARCH))
  97.             ;What you want is not a complete star filename, but
  98.             ; rather the device/directory part of the default used for
  99.             ; LOAD etc.  You don't want the defaults reverting to the
  100.             ; super-defaults applied by the lowest level functions
  101.             ; which access the filesystem.  So, we define:
  102.             (cons ($load_search_dir) (cdr $file_search))
  103.             (CDR MERGE-SPECS))
  104.        (PROBED)
  105.        (FOUND)
  106.        (TRIED))
  107.       ((NULL MERGE-SPECS)
  108.        (IF consp
  109.        `((MLIST) ,@(NREVERSE FOUND))
  110.        (MERROR "Could not MAXIMA-FIND file which matches ~M" X)))
  111.     (IF (DO ((L L (CDR L)) (U ($FILENAME_MERGE (CAR MERGE-SPECS))))
  112.         ((NULL L) NIL)
  113.       (LET ((TRY ($FILENAME_MERGE X U (CAR L))))
  114.         (UNLESS (zl-MEMBER TRY TRIED)
  115.           (PUSH TRY TRIED)
  116.           (IF (SETQ PROBED (PROBE-FILE TRY))
  117.           (IF consp
  118.               (PUSH (TO-MACSYMA-NAMESTRING PROBED) FOUND)
  119.               (RETURN T))))))
  120.     (RETURN (TO-MACSYMA-NAMESTRING PROBED)))))
  121.  
  122. #+cl
  123. (defun lispm-merge-pathname (&rest l)
  124.    (apply 'merge-pathnames  l))
  125.  
  126. ;;; following worked but much harder to search.
  127. ;(defun $file_search (x &optional ign paths)
  128. ;  (or paths (setq paths ign))
  129. ;  (or paths (setq paths  $file_search))
  130. ;  (setq x (macsyma-namestring-sub x))
  131. ;  ;; Fix up $file_search
  132. ;  (and (cdr paths)
  133. ;       (or (pathnamep (second paths))
  134. ;    (setf (cdr paths)
  135. ;          (mapcar  'macsyma-namestring-sub (cdr paths)))))
  136. ;  (sloop for v in (cdr paths)
  137. ;     for tem = (merge-pathnames x v)
  138. ;     when (probe-file tem)
  139. ;     do (return tem)
  140. ;     finally (return (merge-pathnames x))))
  141.  
  142. ;(defun $file_search (x &optional ign paths)
  143. ;  (nth 1 ($file_search1 x ign paths)))
  144. ;
  145. ;(defun $file_search1 (x &optional ign paths)
  146. ;  (or paths (setq paths ign))
  147. ;  (or paths (setq paths  $file_search))
  148. ;  (setq x (macsyma-namestring-sub x))
  149. ;  ;; Fix up $file_search
  150. ;  (and (cdr paths)
  151. ;       (or (pathnamep (second paths))
  152. ;    (setf (cdr paths)
  153. ;          (mapcar  'macsyma-namestring-sub (cdr paths)))))
  154. ;  (sloop for v in (cdr paths)
  155. ;     for tem = (merge-pathnames x v)
  156. ;     do 
  157. ;     (setq tem (directory tem))
  158. ;     (cond (tem (return (cons '(mlist) (nreverse tem)))))))
  159.  
  160.  
  161.  
  162. #-Franz
  163. (defun $load_search_dir ()
  164.   (to-macsyma-namestring *DEFAULT-PATHNAME-DEFAULTS*))
  165.  
  166.  
  167. ;; filename merging is unheard of on Unix.
  168. ;; If the user doesn't supply a file extension, we look for .o, .l and .v
  169. ;; and finally the file itself.  If the user supplies one of the standard
  170. ;; extensions, we just use that.
  171. #+Franz
  172. (defmfun $file_search (x &optional (consp nil) (l $file_types))
  173.    (let ((filelist (cond ((cdr $file_search))
  174.              (t '("."))))
  175.      (extlist (cond ((zl-MEMBER (substring x -2) '(".o" ".l" ".v"))
  176.              '(nil))
  177.             (t '(".o" ".l" ".v" nil)))))
  178.       (do ((dir filelist (cdr dir))
  179.        (ret))
  180.       ((null dir)
  181.        (cond (consp '((mlist)))
  182.              (t (MERROR "Could not MAXIMA-FIND file ~M" X))))
  183.       (cond ((setq ret
  184.                (do ((try extlist (cdr try))
  185.                 (this))
  186.                ((null try))
  187.                (setq this (cond ((null (car try)) x)
  188.                         (t (concat x (car try)))))
  189.                (cond ((not (equal "." (car dir)))
  190.                   (setq this (concat (car dir) "//" this))))
  191.                (cond ((probe-file this)
  192.                   (return
  193.                      (cond (consp `((mlist)
  194.                             ,(to-macsyma-namestring x)))
  195.                         (t (to-macsyma-namestring this))))))))
  196.          (return ret))))))
  197.  
  198.             
  199. (declare-top (SPECIAL $LOADPRINT))
  200.  
  201. (DEFMFUN LOAD-AND-TELL (FILENAME)
  202.   (LOADFILE FILENAME
  203.         T ;; means this is a lisp-level call, not user-level.
  204.         $LOADPRINT))
  205.  
  206. #+PDP10
  207. (PROGN 'COMPILE
  208. ;; on the PDP10 cannonical filenames are represented as symbols
  209. ;; with a DIMENSION-LIST property of DISPLAY-FILENAME.
  210.  
  211. (DEFUN DIMENSION-FILENAME (FORM RESULT)
  212.   (DIMENSION-STRING (CONS #. double-quote-char (NCONC (EXPLODEN FORM) (LIST #. double-quote-char))) RESULT))
  213.  
  214. (DEFUN TO-MACSYMA-NAMESTRING (X)
  215.   ;; create an uninterned symbol, uninterned so that
  216.   ;; it will be GC'd.
  217.   (SETQ X (PNPUT (PNGET (NAMESTRING X) 7) NIL))
  218.   (PUTPROP X 'DIMENSION-FILENAME 'DIMENSION-LIST)
  219.   X)
  220.  
  221. (DEFUN MACSYMA-NAMESTRINGP (X)
  222.   (AND (SYMBOLP X) (EQ (GET X 'DIMENSION-LIST) 'DIMENSION-FILENAME)))
  223.  
  224. (DEFMACRO ERRSET-NAMESTRING (X)
  225.   `(LET ((ERRSET NIL))
  226.      (ERRSET (NAMESTRING ,X) NIL)))
  227.  
  228. (DEFMFUN $FILENAME_MERGE N
  229.   (DO ((F "" (MERGEF (MACSYMA-NAMESTRING-SUB (ARG J)) F))
  230.        (J N (f1- J)))
  231.       ((ZEROP J)
  232.        (TO-MACSYMA-NAMESTRING F))))
  233. )
  234.  
  235. #+Franz
  236. (progn 'compile
  237.  
  238. ;; a first crack at these functions
  239.  
  240. (defun to-macsyma-namestring (x)
  241.    (cond ((macsyma-namestringp x) x)
  242.      ((symbolp x)
  243.       (cond ((memq (getcharn x 1) '(#\& #\$))
  244.          (substring (get_pname x) 2))
  245.         (t (get_pname x))))
  246.      (t (merror "to-macsyma-namestring: non symbol arg ~M~%" x))))
  247.  
  248. (defun macsyma-namestringp (x)
  249.    (stringp x))
  250.  
  251. ;;--- $filename_merge
  252. ; may not need this ask filename merging is not done on Unix systems.
  253. ;
  254. (defmfun $filename_merge (&rest files)
  255.    (cond (files (filestrip (ncons (car files))))))
  256. )
  257.  
  258. #+MULTICS
  259. (PROGN 'COMPILE
  260. (DEFUN TO-MACSYMA-NAMESTRING (X) 
  261.   (cond ((macsyma-namestringp x) x)
  262.     ((symbolp x) (substring (string x) 1))
  263.     ((consp x) (namestring x))
  264.     (t x)))
  265.  
  266. (DEFUN MACSYMA-NAMESTRINGP (X) (STRINGP X))
  267. (DEFUN ERRSET-NAMESTRING (X)
  268.   (IF (ATOM X) (NCONS (STRING X)) (ERRSET (NAMESTRING X) NIL)))
  269.  
  270. (DEFMFUN $FILENAME_MERGE (&REST FILE-SPECS)
  271.   (SETQ FILE-SPECS (cond (file-specs 
  272.               (MAPCAR #'MACSYMA-NAMESTRING-SUB FILE-SPECS))
  273.              (t '("**"))))
  274.   (TO-MACSYMA-NAMESTRING (IF (NULL (CDR FILE-SPECS))
  275.                  (CAR FILE-SPECS)
  276.                  (APPLY #'MERGEF FILE-SPECS))))
  277.  
  278. )
  279.  
  280. #+(and (not cl) Lispm)
  281. (PROGN 'COMPILE
  282. (DEFUN TO-MACSYMA-NAMESTRING (X)
  283.   (PATHNAME X)
  284.   )
  285. (DEFUN MACSYMA-NAMESTRINGP (X)
  286.   (ml-typep X 'FS:PATHNAME))
  287. (DEFUN ERRSET-NAMESTRING (X)
  288.   (LET ((ERRSET NIL))
  289.     (ERRSET (PATHNAME X) NIL)))
  290.  
  291. (DEFMFUN $FILENAME_MERGE (&REST FILE-SPECS)
  292.   (SETQ FILE-SPECS (cond (file-specs 
  293.               (MAPCAR #'MACSYMA-NAMESTRING-SUB FILE-SPECS))
  294.              (t '("**"))))
  295.   ($file_search
  296.      (TO-MACSYMA-NAMESTRING (IF (NULL (CDR FILE-SPECS))
  297.                  (CAR FILE-SPECS)
  298.                  (APPLY #'MERGEF FILE-SPECS)))
  299.  
  300.   ))
  301.  
  302.  
  303. #+cl
  304. (PROGN 'COMPILE
  305. (DEFUN TO-MACSYMA-NAMESTRING (X)
  306.   (PATHNAME X)
  307.   )
  308. (DEFUN MACSYMA-NAMESTRINGP (X)
  309.   (typep X 'PATHNAME))
  310. (DEFUN ERRSET-NAMESTRING (X)
  311.   (LET ((ERRSET NIL))
  312.     (ERRSET (PATHNAME X) NIL)))
  313.  
  314. (DEFMFUN $FILENAME_MERGE (&REST FILE-SPECS)
  315.   (SETQ FILE-SPECS (cond (file-specs 
  316.               (MAPCAR #'MACSYMA-NAMESTRING-SUB FILE-SPECS))
  317.              (t '("**"))))
  318.  
  319.   (progn            ;$file_search
  320.      (TO-MACSYMA-NAMESTRING (IF (NULL (CDR FILE-SPECS))
  321.                  (CAR FILE-SPECS)
  322.                  (APPLY #'MERGEF FILE-SPECS)))
  323.  
  324.   )
  325.   
  326.   )
  327.  
  328.  
  329. ;
  330. ;#+NIL
  331. ;(PROGN 'COMPILE
  332. ;(DEFUN TO-MACSYMA-NAMESTRING (X)
  333. ;  (PATHNAME X))
  334. ;(DEFUN MACSYMA-NAMESTRINGP (X)
  335. ;  (ml-typep X 'PATHNAME))
  336. ;(DEFUN ERRSET-NAMESTRING (X)
  337. ;  (LET ((ERRSET NIL))
  338. ;    (ERRSET (PATHNAME X) NIL)))
  339. ;
  340. ;(DEFMFUN $FILENAME_MERGE (&RESTV FILE-SPECS)
  341. ;  ;For this crufty, it is better to just do the dumb component-by-component
  342. ;  ; merging rather than trusting the allegedly winning heuristics of
  343. ;  ; merge-pathname-defaults.
  344. ;  (do ((i (f1- (length file-specs)) (f1- i))
  345. ;       (p "" (merge-pathnames
  346. ;           (macsyma-namestring-sub (svref file-specs i)) p)))
  347. ;      ((< i 0) (to-macsyma-namestring p))))
  348. ;) ;#+NIL
  349.  
  350. (DEFUN MACSYMA-NAMESTRING-SUB (USER-OBJECT)
  351.   (IF (MACSYMA-NAMESTRINGP USER-OBJECT) USER-OBJECT
  352.       (LET* ((SYSTEM-OBJECT
  353.           (COND ((AND (ATOM USER-OBJECT) (NOT (SYMBOLP USER-OBJECT)))
  354.              USER-OBJECT)
  355.             ((and (symbolp user-object)
  356.               (eql #\$ (getcharn user-object 1)))
  357.              (string-downcase (fullstrip1 user-object)))
  358.             ((ATOM USER-OBJECT)
  359.              (FULLSTRIP1 USER-OBJECT))
  360.             (($LISTP USER-OBJECT)
  361.              (FULLSTRIP (CDR USER-OBJECT)))
  362.             (T
  363.              (MERROR "Bad file spec:~%~M" USER-OBJECT))))
  364.          (NAMESTRING-TRY (ERRSET-NAMESTRING SYSTEM-OBJECT)))
  365.     (IF NAMESTRING-TRY (CAR NAMESTRING-TRY)
  366.         ;; know its small now, so print on same line.
  367.         (MERROR "Bad file spec: ~:M" USER-OBJECT)))))
  368.  
  369. (DEFMFUN open-out-dsk (x)
  370.   #+(or cl nil)
  371.   (open x :direction :output :element-type 'character)
  372.  #-(or CL NIL)   (open x '(out dsk ascii block)))
  373.  
  374. (DEFMFUN open-in-dsk (x)
  375.      #+(or cl nil)
  376.      (open x :direction :input :element-type 'character)
  377.          #-(or CL NIL) (open x '(in dsk ascii block)))
  378.  
  379. #-MAXII
  380. (PROGN 'COMPILE
  381.  
  382. (declare-top (SPECIAL DSKFNP OLDST ST $NOLABELS REPHRASE))
  383.  
  384. (DEFMFUN CALL-BATCH1 (FILENAME ^W)
  385.   (LET ((^R (AND ^R (NOT ^W)))
  386.     ($NOLABELS T)
  387.     ($CHANGE_FILEDEFAULTS)
  388.     (DSKFNP T)
  389.     (OLDST)
  390.     (ST))
  391.     ;; cons #/& to avoid the double-stripdollar problem.
  392.     (BATCH1 (LIST (MAKNAM (CONS #\& (EXPLODEN FILENAME))))
  393.         NIL
  394.         NIL
  395.         #-Franz T
  396.         #+Franz nil)
  397.     (SETQ REPHRASE T)))
  398.  
  399.  
  400. (DEFMVAR *IN-$BATCHLOAD* NIL
  401.   "I should have a single state variable with a bit-vector or even a list
  402.   of symbols for describing the state of file translation.")
  403. (DEFMVAR *IN-TRANSLATE-FILE* NIL "")
  404. (DEFMVAR *IN-MACSYMA-INDEXER* NIL)
  405.  
  406. (DEFUN TRANSLATE-MACEXPR (FORM &optional FILEPOS)
  407.        (COND (*IN-TRANSLATE-FILE*
  408.           (TRANSLATE-MACEXPR-ACTUAL FORM FILEPOS))
  409.          (*in-macsyma-indexer*
  410.           (outex-hook-exp form))
  411.          (T
  412.           (LET ((R (ERRSET (MEVAL* FORM))))
  413.            (COND ((NULL R)
  414.               (LET ((^W NIL))
  415.                    (MERROR "~%This form caused an MAXIMA-ERROR in evaluation:~
  416.                        ~%~:M" FORM))))))))
  417. )
  418.  
  419.  
  420.  
  421.  
  422. ;(DEFMFUN $BATCHLOAD (FILENAME &aux)
  423. ;  (LET ((WINP NIL)
  424. ;    (NAME ($FILENAME_MERGE FILENAME))
  425. ;    (*IN-$BATCHLOAD* T))
  426. ;    (IF $LOADPRINT
  427. ;    (MTELL "~%Batching the file ~M~%" NAME))
  428. ;    (UNWIND-PROTECT
  429. ;     (PROGN (CALL-BATCH1 NAME T)
  430. ;        (SETQ WINP T)
  431. ;        NAME)
  432. ;     ;; unwind protected.
  433. ;     (IF WINP
  434. ;     (IF $LOADPRINT (MTELL "Batching done."))
  435. ;     (MTELL "Some MAXIMA-ERROR in loading this file: ~M" NAME)))))
  436. ;
  437. ;;; end of moby & crufty #-MAXII
  438. ;)
  439. ;
  440. ;#+(and MAXII (not cl))
  441. ;(DEFMFUN $BATCHLOAD (FILENAME)
  442. ;  (LET ((EOF (LIST NIL))
  443. ;    (NAME ($FILENAME_MERGE FILENAME))
  444. ;    (*MREAD-PROMPT* "(Batching) "))
  445. ;    ;I'm not about to try and figure out why the first ~% in this is a no-op.
  446. ;    ; MFORMAT is about as opaque as i have ever seen.
  447. ;    ;(IF $LOADPRINT (MTELL "~%Batching the file ~M~%" NAME))
  448. ;    (when $loadprint (format t "~&Batching the file ~A.~%" name))
  449. ;    (WITH-OPEN-FILE (STREAM NAME '(:IN :ASCII))
  450. ;      (DO ((FORM NIL (MREAD STREAM EOF)))
  451. ;      ((EQ FORM EOF)
  452. ;       ;(IF $LOADPRINT (MTELL "Batching done."))
  453. ;       (when $loadprint (format t "~&Batching done.~%"))
  454. ;       '$DONE)
  455. ;    (MEVAL* (CADDR FORM))))))
  456.  
  457.  
  458.  
  459. #+cl
  460. (defun $batchload (filename &aux expr (*mread-prompt* "") )
  461.   (setq filename ($file_search1 filename '((mlist) $file_search_maxima)))
  462.   (with-open-file (in-stream filename)
  463.           (when $loadprint (format t "~&batching ~A"
  464.                        (lisp::namestring
  465.                         (truename in-stream))))
  466.           (cleanup)
  467.           (newline in-stream #\n)
  468.           (sloop while (and
  469.                 (setq  expr (mread in-stream nil))
  470.                 (consp expr))
  471.              do (meval* (third expr)))
  472.           (lisp::namestring (truename in-stream))))
  473.  
  474. ;;returns appropriate error or existing pathname.
  475. ;; the second argument is a maxima list of variables
  476. ;; each of which contains a list of paths.   This is
  477. ;; so users can correct the variable..
  478. (defun $file_search1 (name search-lists &aux lis)
  479.   (if (pathnamep name) (setq name (namestring name)))
  480.   (setq lis (apply '$append (mapcar 'symbol-value (cdr search-lists))))
  481.   (let ((res ($file_search name lis)))
  482.     (or res
  483.     (merror "Could not find `~M' using paths in ~A (combined values: ~M )"
  484.         name    (string-trim "[]" ($sconcat search-lists)) lis))))
  485.  
  486. (DEFMFUN $LOAD (filename)
  487.   "This is the generic file loading function.
  488.   LOAD(filename) will either BATCHLOAD or LOADFILE the file,
  489.   depending on wether the file contains Macsyma, Lisp, or Compiled
  490.   code. The file specifications default such that a compiled file
  491.   is searched for first, then a lisp file, and finally a macsyma batch
  492.   file. This command is designed to provide maximum utility and
  493.   convenience for writers of packages and users of the macsyma->lisp
  494.   translator."
  495.  
  496.   (cond ((and (symbolp filename) (not (mstringp filename)))
  497.      (setq filename (string-downcase (symbol-name (stripdollar filename))))))
  498.   (LET ((SEARCHED-FOR
  499.      ($file_search1 filename
  500.             '((mlist) $file_search_maxima $file_search_lisp  )))
  501.      type)
  502.     (setq TYPE ($FILE_TYPE SEARCHED-FOR))
  503.     (CASE TYPE
  504.           (($MAXIMA)
  505.            ($BATCHLOAD SEARCHED-FOR))
  506.           (($LISP $OBJECT)
  507.            ;; do something about handling errors
  508.            ;; during loading. Foobar fail act errors.
  509.            (LOAD-AND-TELL SEARCHED-FOR))
  510.           (T
  511.            (MERROR "MACSYMA BUG: Unknown file type ~M" TYPE)))
  512.     searched-for
  513.     ))
  514.  
  515. ;#+Multics ;; not so many multics machines around today.
  516. ;(DEFMFUN $FILE_TYPE (FILE)
  517. ;  (SETQ FILE ($FILENAME_MERGE FILE))
  518. ;  (IF (NULL (PROBE-FILE FILE)) NIL
  519. ;      (CASE (CAR (LAST (NAMELIST FILE)))
  520. ;    ((MACSYMA) '$MACSYMA)
  521. ;    ((LISP) '$LISP)
  522. ;    (T '$FASL))))
  523.  
  524. #-(or MULTICS NIL cl)
  525. (DEFMFUN $FILE_TYPE (FILENAME &AUX STREAM)
  526.   (SETQ FILENAME ($FILENAME_MERGE FILENAME))
  527.   (COND ((NULL (PROBE-FILE FILENAME))
  528.      NIL)
  529. #-Franz ((FASLP FILENAME)
  530.      '$FASL)
  531. #+Franz ((cdr (zl-ASSOC (substring filename -2)
  532.              '((".l" . $lisp) (".o" . $fasl) (".v" . $macsyma)))))
  533.     ('ELSE
  534.      ;; This has to be simple and small for greatest utility
  535.      ;; as an in-core pdp10 function.
  536.      (UNWIND-PROTECT
  537.       (DO ((C (PROGN (SETQ STREAM (OPEN-IN-DSK FILENAME))
  538.              #\SPACE)
  539.           (TYI STREAM -1)))
  540.           ((NOT (zl-MEMBER C '(#\SPACE #\TAB #\Return #\Linefeed #\Page)))
  541.            ;; heuristic number one,
  542.            ;; check for cannonical language "comment." as first thing
  543.            ;; in file after whitespace.
  544.            (COND ((zl-MEMBER C '(-1 #. semi-colon-char))
  545.               '$LISP)
  546.              ((AND (= C #. forward-slash-char)
  547.                (= (TYI STREAM -1) #\*))
  548.               '$MACSYMA)
  549.        #+Franz   ((eq c 7)        ;; fasl files begin with bytes 7,1
  550.               '$fasl)        ;; but just seeing 7 is good enough
  551.              ('ELSE
  552.               ;; the above will win with all Lisp files written by
  553.               ;; the macsyma system, e.g. the $SAVE and
  554.               ;; $TRANSLATE_FILE commands, all lisp files written
  555.               ;; by macsyma system programmers, and anybody else
  556.               ;; who starts his files with a "comment," lisp or
  557.               ;; macsyma.
  558.               (FILEPOS STREAM 0)
  559.               ;; heuristic number two, see if READ returns something
  560.               ;; evaluable.
  561.               (LET ((FORM (LET ((ERRSET NIL))
  562.                     ;; this is really bad to do since
  563.                     ;; it can screw the lisp programmer out
  564.                     ;; of a chance to identify read errors
  565.                     ;; as they happen.
  566.                     (ERRSET (READ STREAM NIL) NIL))))
  567.             (IF (OR (NULL FORM)
  568.                 (ATOM (CAR FORM)))
  569.                 '$MACSYMA
  570.                 '$LISP))))))
  571.       ;; Unwind protected.
  572.       (IF STREAM (CLOSE STREAM))))))
  573.  
  574.  
  575.  
  576.  
  577. (defun $mkey (keyword)
  578.     "takes a macsyma symbol and makes a keyword of it"
  579.   (intern (string-left-trim "$" (string-upcase (string keyword))) 'keyword))
  580.  
  581.  
  582. (defun quote-simple-equal (f g)
  583.   (and (consp f) (eql (caar f) 'mquote)
  584.        (cond ((atom g) (equal (second f) g))
  585.          ((not (eql (caar g) 'mquote))
  586.           ($simple_equal (second f) g))
  587.          (t ($simple_equal (cdr f) (cdr g))))))
  588.  
  589. (defun unequal-pairlis (var gen)
  590.   (sloop for v in var
  591.     for u in gen
  592.     collecting (cons v u)))
  593.  
  594. (defun $simple_equal (f g)
  595.   "checks if equal up to simp flags"
  596.   (cond    ((quote-simple-equal f g))
  597.     ((quote-simple-equal g f))
  598.     ((and  (numberp f) (numberp g)
  599.            (or (eql f g))
  600.            (< (- f g) 1.0e-4)))
  601.         ((atom f) (equal f g))
  602.     ((atom g) nil)
  603.     ((eql (caar f) 'mrat)
  604.      (equal(fifth (car f)) (fifth (car g)))
  605.      (equal (sublis (unequal-pairlis  (third (car f))
  606.                  (fourth (car f)))
  607.             (cdr f))
  608.          (sublis (unequal-pairlis (third (car g))
  609.                  (fourth (car g)))
  610.             (cdr g))))
  611.     (t(and (equal (caar f) (caar g))
  612.            (eql (length f) (length g))
  613.            (sloop for v in (cdr f)
  614.              for w in (cdr g)
  615.              when (not ($simple_equal v w))
  616.                do (return nil)
  617.               finally (return t))))))
  618.  
  619.  
  620. #+cl
  621. (defun $file_type (fil &aux typ)
  622.   (setq fil ( pathname          fil))
  623.   (setq typ (format nil "~(~A~)" (pathname-type fil)))
  624.   (or 
  625.    (and (> (length typ) 0)
  626.        (let ((ch (aref typ 0)))
  627.      (cdr (assoc ch '( (#\m . $maxima) (#\d . $maxima) (#\l . $lisp)
  628.             (#\o . $object)
  629.             (#\f . $object))))))
  630.    '$object))
  631.                    
  632.  
  633. #+NIL
  634. (DEFVAR *EDITOR-MODE-TO-MACSYMA-FILE-TYPE-TABLE*
  635.   '((:MACSYMA . $MACSYMA)
  636.     (:LISP . $LISP)
  637.     (:LSB . $LISP)))
  638.  
  639. #+NIL
  640. (DEFVAR *PATHNAME-TYPE-TO-MACSYMA-FILE-TYPE-TABLE*
  641.   '(("LISP" . $LISP)
  642.     ("MC" . $MACSYMA)    ;In case the pathname code doesn't know this inversion
  643.     ("MACSYMA" . $MACSYMA)
  644.     ("VASL" . $FASL)))
  645.  
  646. #+NIL
  647. (DEFMFUN $FILE_TYPE (FILENAME &AUX TEM DISEMBODIED)
  648.   (SETQ FILENAME ($FILENAME_MERGE FILENAME))
  649.   (COND ((NULL (SETQ DISEMBODIED (SEND (PATHNAME FILENAME) :FILE-PLIST))) NIL)
  650.     ((GET DISEMBODIED :VASLP) '$FASL)
  651.     ((CDR (ASSQ (GET DISEMBODIED :MODE)
  652.             *EDITOR-MODE-TO-MACSYMA-FILE-TYPE-TABLE*)))
  653.     ((CDR (SYS:ASSOC (SEND (CAR DISEMBODIED) :TYPE)
  654.              *PATHNAME-TYPE-TO-MACSYMA-FILE-TYPE-TABLE*
  655.              :TEST #'STRING-EQUAL)))))
  656.  
  657. #-cl
  658. (DEFMVAR $FILE_SEARCH
  659.   '((MLIST)))
  660.  
  661. #-cl
  662. (defun initialize-$file_search ()
  663.   (setq $file_search
  664.     #+ITS
  665.     `((MLIST)
  666.       ,@(MAPCAR #'TO-MACSYMA-NAMESTRING
  667.             `(,`((dsk ,(status homedir)))
  668.               "DSK:SHARE;" "DSK:SHARE1;" "DSK:SHARE2;" "DSK:SHAREM;")))
  669.     #+NIL
  670.     `((mlist) ,@(let ((wdir (user-workingdir-pathname))
  671.               (hdir (user-homedir-pathname)))
  672.               (cons wdir (and (not (equal wdir hdir)) (list hdir))))
  673.           ,(to-macsyma-namestring "max$disk:[share]")
  674.           ,(to-macsyma-namestring "max$disk:[share1]")
  675.           ,(to-macsyma-namestring "max$disk:[share2]")
  676.           ,(to-macsyma-namestring "max$disk:[sharem]"))
  677.     #+Franz
  678.     `((mlist)
  679.       ,@(mapcar #'to-macsyma-namestring
  680.             `("."
  681.               ,(concat vaxima-main-dir "//share")
  682.               ,(concat vaxima-main-dir "//demo"))))
  683.     #+CL
  684.     `((MLIST))
  685.     
  686.     #+Multics
  687.       (LET ((WHERE-AM-I (CAR (NAMELIST EXECUTABLE-DIR))))
  688.         `((MLIST) 
  689.           ,@(mapcar 
  690.          #'to-macsyma-namestring
  691.          `(,(string-append (PATHNAME-UTIL "hd") ">**")
  692.            ,(string-append (NAMESTRING `(,WHERE-AM-I "share")) ">**")
  693.            ,(string-append (NAMESTRING `(,WHERE-AM-I "executable"))
  694.                    ">**")))))))
  695.  
  696. (defvar *macsyma-startup-queue* nil)
  697. ;(push '(initialize-$file_search) *macsyma-startup-queue*)
  698.  
  699. (eval-when (compile) (proclaim '(special *mread-prompt*)))
  700.  
  701.  
  702. ;; Done for debuggings sake.
  703. ;(eval-when (eval load)  (initialize-$file_search))
  704.  
  705. (defmfun mfilename-onlyp (x)
  706.   "Returns T iff the argument could only be reasonably taken as a filename."
  707.   (cond ((macsyma-namestringp x) t)
  708.     (($listp x) t)
  709.     ((symbolp x)
  710.      (char= #\& (getcharn x 1)))
  711.     ('else
  712.      nil)))
  713.  
  714.  
  715. ;;;; batch & demo search hacks
  716.  
  717. #+cl
  718. (defun $batch (filename &optional (demo :batch)
  719.             &aux tem   (possible '(:demo :batch :test)))
  720.   "giving a second argument makes it use demo mode, ie pause after evaluation
  721.    of each command line"
  722.   (cond ((setq tem (memq ($mkey demo) possible))
  723.      (setq demo (car tem)))
  724.     (t (format t "Second arg ~A is not in ~A so using :Batch"
  725.            demo possible)))
  726.  
  727.   (setq filename ($file_search1 filename
  728.                 (if (eql demo :demo)
  729.                     '((mlist) $file_search_demo )
  730.                   '((mlist) $file_search_maxima ))))
  731.   (cond ((eq demo :test)
  732.      (test-batch filename))
  733.     (t
  734.      (with-open-file (in-stream filename)
  735.        (format t "~%batching ~A"
  736.         (truename in-stream))
  737.        (continue in-stream demo)
  738.        (namestring in-stream)))))
  739.  
  740. (defvar *collect-errors* t)
  741.  
  742. ;(defun you-supply (description &key return-string 
  743. ;                  default-value (stream *query-io*) &aux answ )
  744. ;   (cond (return-string (format stream "~%Supply a value for  ~A: " description))
  745. ;     (t (format stream "~%Supply a form to evaluate to use for ~A: " description)))
  746. ;;  (apply 'format stream format-string format-arg-list)
  747. ;  (cond (default-value (format stream "(default is: ~A)" default-value)))
  748. ;  (setq answ (read-line stream))
  749. ;  (cond ((equal answ "")default-value)
  750. ;    (return-string answ)
  751. ;    (t (eval (read-from-string answ)))))
  752.  
  753. ;(defun query-pathname (&key make-pathname-options (description "the pathname") (default-pathname "foo.lisp"))
  754. ;  (setq name (pathname (you-supply description :default-value (apply 'alter-pathname default-pathname make-pathname-options)
  755. ;                   :return-string t)))
  756. ;      (apply 'alter-pathname name make-pathname-options))
  757.     
  758.  
  759. (defun test-batch (filename &optional (out *standard-output*) &aux result next-result  next eof error-log all-differences
  760.                 (*mread-prompt* "")
  761.                 ($matrix_element_mult '&*))
  762.   (cond (*collect-errors*
  763.      (setq error-log
  764.            (if (streamp *collect-errors*) *collect-errors*
  765.          (open (alter-pathname filename :type "ERR")
  766.                :direction :output)))
  767.      (format t "~%Error log on ~a" error-log)
  768.      (format error-log "~%/*    MAXIMA-ERROR log for testing of ~A" filename)
  769.      (format error-log "*/~2%")))
  770.   
  771.   (unwind-protect 
  772.       (with-open-file
  773.        (st filename)
  774.        (sloop with expr 
  775.           for i from 1
  776.           do
  777.           (setq expr (mread st eof))
  778.           while expr 
  779.           do
  780.           (format out "~%/* ********************** Problem ~A. *************** */ " i)
  781.                     ;      (mshow expr)
  782.           (format out "~%%Input is" )
  783.           (displa (third expr))
  784.           (setq $%(setq result  (meval* (third expr))))
  785.           (format out "~%~%The result is")
  786.           (displa $%)
  787.           (setq next (mread st eof))
  788.           (cond ((null next) (error "no result")))
  789.           (setq next-result  (third next))
  790.           (cond ((batch-equal-check next-result result)
  791.              (format t "~%..Which was correct"))
  792.             (t (format t "~%This differed from the expected result:")
  793.                (push i all-differences)
  794.                (displa next-result)
  795.                (cond (*collect-errors*
  796.                   (mgrind (third expr) error-log)
  797.                   (list-variable-bindings (third expr) error-log)
  798.                   (format error-log ";~%")
  799.                   (format error-log "//*Erroneous Result?:~%")
  800.                   (mgrind result error-log) (format error-log "*// ")
  801.                   (terpri error-log)
  802.                   (mgrind next-result error-log)
  803.                   (format error-log ";~%~%"))))
  804.             )))
  805.     (cond (error-log
  806.        (or (streamp *collect-errors*)
  807.            (close error-log)))))
  808.   (cond ((null all-differences)
  809.      (format t "~%Congratulations: No differences!") '((mlist)))
  810.     (t (format t "~%The number of differences found was ~A in problems: ~A" (length all-differences)
  811.            all-differences)
  812.        `((mlist),filename ,@ all-differences))))
  813.        
  814.  
  815. (defun batch-equal-check (next-result result &optional recursive)
  816.   (or (like next-result result)
  817.       ($simple_equal next-result result)
  818.       (cond ((not
  819.           (or recursive ($simple_equal result $functions)))
  820.          (batch-equal-check (meval* next-result)  result ;(meval* result)
  821.                 t )))
  822.       (and (not ($bfloatp result))
  823.        (let (($fpprec 12))
  824.          (declare (special $fpprec))
  825.          (equal (mstring result) (mstring next-result))
  826.          ))
  827.  
  828.          
  829.       (cond ((not  (appears-in result 'factored))
  830.          (format t "~%Using ratsimp")(like ($ratsimp next-result)
  831.                            ($ratsimp result))))
  832.       (equal 0 ($ratsimp `((mplus) ,next-result ((mtimes) -1 ,result))))
  833.       (equal (msize result nil nil nil nil )
  834.          (msize next-result nil nil nil nil))
  835.  
  836.       ))
  837.  
  838. ;;to keep track of global values during the error:
  839. (defun list-variable-bindings (expr &optional str &aux tem)
  840.   (sloop for v in(cdr ($listofvars  expr))
  841.     when (zl-MEMBER v $values)
  842.     collecting (setq tem`((mequal) ,v ,(meval* v)))
  843.         and
  844.     do (cond (str (format str ",")(mgrind tem str)))))
  845.  
  846. ;;in init_max
  847. ;; name = foo or foo.type or dir/foo.type or dir/foo 
  848. ;; the empty parts are filled successively from defaults in templates in
  849. ;; the path.   A template may use multiple {a,b,c} constructions to indicate
  850. ;; multiple possiblities.  eg foo.l{i,}sp or foo.{dem,dm1,dm2}
  851. (defun $file_search (name &optional paths)
  852.   (if (and (symbolp name)
  853.        (member (getcharn name 1) '(#\& #\$)))
  854.       (setq name (subseq (symbol-name name) 1)))
  855.   (if (symbolp name)  (setf name (string name)))
  856.   (if (probe-file name) (return-from $file_search name))
  857.   (or paths (setq paths ($append $file_search_lisp  $file_search_maxima
  858.                  $file_search_demo)))
  859.   (atomchk paths '$file_search t)
  860.   (new-file-search (string name) (cdr paths)))
  861.  
  862. (defun new-file-search (name template &aux lis temp)
  863.   (cond ((probe-file name))
  864.     ((atom template)
  865.      (setq template (namestring ($filename_merge template  name)))
  866.      ;(print (list 'template template))
  867.      (setq lis 
  868.             (sloop for w in (split-string template "{}")
  869.                      when (null (position #\, w))
  870.                      collect w
  871.                      else
  872.                      collect (split-string w ",")))
  873.      (new-file-search1 "" lis))
  874.     (t (sloop for v in template
  875.           when (setq temp (new-file-search name v))
  876.           do (return temp)))))
  877.  
  878. (defun new-file-search1 (begin lis )
  879.   (cond ((null lis) (if (probe-file begin) begin nil))
  880.     ((atom (car lis))
  881.      (new-file-search1 (if begin
  882.                    ($sconcat begin (car lis)) (car lis))
  883.                (cdr lis)))
  884.     (t (sloop for v in (car lis) with tem
  885.           when (setq tem  (new-file-search1 begin (cons v (cdr lis))))
  886.           do (return tem)))))
  887.  
  888. (defun save-linenumbers (&key (c-lines t) d-lines (from 1) (below $linenum) a-list
  889.                   (file  "/tmp/lines")
  890.                   &aux input-symbol (linel 79))
  891.   (cond ((null a-list) (setq a-list(sloop for i from from below below collecting i))))
  892.   (with-open-file  (st file  #-cl '( :out) #+cl :direction #+cl :output  )
  893.     (format st "/* -*- Mode: MACSYMA; Package: MACSYMA -*- */")
  894.     (format st "~%~%       /*    ~A     */  ~%"
  895.         #+lispm (time::print-current-time nil)
  896.         #-lispm (let ((tem (cdddr
  897.                 (multiple-value-list (get-decoded-time)))))
  898.               (format nil "~a:~a:~a" (car tem) (cadr tem) (caadr tem)))
  899.         )
  900.         (sloop for i in a-list
  901.          when (and c-lines (boundp (setq input-symbol (intern (format nil "$C~A" i)))))
  902.          do
  903.          (format st "~% C~3A;  "   i)
  904.          (mgrind (symbol-value input-symbol)
  905.              st)
  906.          (format st ";")
  907.          when (and d-lines
  908.          (boundp (setq input-symbol (intern (format nil "$D~A" i)))))
  909.          do
  910.          (format st "~% D~3A:  "   i)
  911.          (mgrind (symbol-value
  912.                input-symbol)
  913.              st)
  914.          (format st "$"))))
  915.  
  916.  
  917. (defun $printfile (file)
  918.   (setq file ($file_search1 file '((mlist) $file_search_usage)))
  919.   (with-open-file (st file)
  920.           (sloop while (setq tem (read-char st)) with tem
  921.              do
  922.              (princ tem))
  923.           (namestring file)
  924.           ))
  925.  
  926.   
  927.   
  928.